package com.ysd.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ysd.common.entity.Img;
import com.ysd.common.entity.Product;
import com.ysd.common.entity.ProductEChart;
import com.ysd.product.mapper.ProductMapper;
import com.ysd.product.service.ProductService;

import net.sf.json.JSONArray;
@Service
@Transactional(readOnly = true)
public class ProductServiceImpl implements ProductService{
	@Autowired
	private ProductMapper productMapper;
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findAllBySelAndPage(Product product, int page, int limit) {
		// TODO Auto-generated method stub
		return productMapper.findAllBySelAndPage(product.getName(),product.getF_pcid(),product.getIsxj(),product.getIsxss(),product.getIsxsg(),page,limit);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int findProductTotal(Product product, int page, int limit) {
		// TODO Auto-generated method stub
		return productMapper.findProductTotal(product.getName(),product.getF_pcid(),product.getIsxj(),product.getIsxss(),product.getIsxsg(),page,limit);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int saveProduct(Product product) {
		// TODO Auto-generated method stub
		return productMapper.saveProduct(product);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int updateProduct(Product product) {
		// TODO Auto-generated method stub
		return productMapper.updateProduct(product);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int delProduct(int id) {
		// TODO Auto-generated method stub
		return productMapper.delProduct(id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int updateProductIsXss(int isxss, int id) {
		// TODO Auto-generated method stub
		return productMapper.updateProductIsXss(isxss, id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int updateProductIsXsg(int isxsg, int id) {
		// TODO Auto-generated method stub
		return productMapper.updateProductIsXsg(isxsg, id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int updateProductIsXj(int isxj, int id) {
		// TODO Auto-generated method stub
		return productMapper.updateProductIsXj(isxj, id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public Map<String, Object> findProductById(String str) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		JSONArray json = JSONArray.fromObject(str); 
		@SuppressWarnings("unchecked")
		List<Product> productlist=(List<Product>)JSONArray.toCollection(json, Product.class);
		for(int i=0;i<productlist.size();i++) {
			Map<String, Object> map1=new HashMap<String, Object>();
			Product p=productlist.get(i);
			Product product=productMapper.findProductById(p.getId());
			if(product.getNewprice()-p.getNewprice()!=0) {
				map1.put("message", "价格已经变动");
				map1.put("id", p.getId());
				map1.put("success", false);
			}else if(product.getNum()-p.getNum()<0) {
				map1.put("message", "库存不足");
				map1.put("id", p.getId());
				map1.put("success", false);
			}else if(product.getIsxj()==1) {
				map1.put("message", "商品已经下架");
				map1.put("id", p.getId());
				map1.put("success", false);
			}else {
				map1.put("message", "成功");
				map1.put("success", true);
			}
			map.put("msg"+i, map1);
		}		
		return map;
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findAllProByIndex() {
		// TODO Auto-generated method stub
		return productMapper.findAllProByIndex();
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findProBySellNum() {
		// TODO Auto-generated method stub
		return productMapper.findProBySellNum();
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findAllProByProlist(int f_pcid,String key,int page, int limit) {
		// TODO Auto-generated method stub
		return productMapper.findAllProByProlist(f_pcid,key, page, limit);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int findAllProByProlistTotal(int f_pcid,String key, int page, int limit) {
		// TODO Auto-generated method stub
		return productMapper.findAllProByProlistTotal(f_pcid,key, page, limit);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<ProductEChart> findProCateEChart() {
		// TODO Auto-generated method stub
		return productMapper.findProCateEChart();
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public Product findProById(int id) {
		// TODO Auto-generated method stub
		return productMapper.findProById(id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Img> findAllImgByPId(int id) {
		// TODO Auto-generated method stub
		return productMapper.findAllImgByPId(id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int addProImgs(Img imgs) {
		// TODO Auto-generated method stub
		return productMapper.addProImgs(imgs);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public int delProImg(int id) {
		// TODO Auto-generated method stub
		return productMapper.delProImg(id);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findProBySNum() {
		// TODO Auto-generated method stub
		return productMapper.findProBySNum();
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public List<Product> findAllProByIsxsg() {
		// TODO Auto-generated method stub
		return productMapper.findAllProByIsxsg();
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
	public Product findProCateNameById(int pcid) {
		// TODO Auto-generated method stub
		return productMapper.findProCateNameById(pcid);
	}

	
	

}
